<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server Sent Event Demo</title>
<body>
<form onsubmit="ajaxSent(); return false;">
	<p><input type="button" id="btn" value="Stop SSE" /></p>
	<p><input type="submit" /></p>
</form>

<script type="text/javascript">
window.onload=function() {
	var _evtSource;
	_evtSource = new EventSource("sse.php");
	//_evtSource.addEventListener("ping", function(e) {

	_evtSource.onmessage = function(e) {
		//console.log(e);

		//alert (e);
		var obj = JSON.parse(e.data);
		console.log(obj);
		//if (obj.optid == "approved") {
		//	error('kuku;');
			//window.location.reload();
			//_evtSource.close();
		//}
	}
	
	var el = document.getElementById("btn");
	el.addEventListener("click", function(e) {_evtSource.close()}, false);
	
	//)
}

ajaxSent=function(){
	try{
         xhr=new XMLHttpRequest();
    }
    catch(err){
         alert(err);
    }
	msg = 'kuku';
	name = 'roman';
    xhr.open('GET', 'sse-ob.php?msg=' + msg + '&name=' + name, false);
    xhr.onreadystatechange = function(){
         if(xhr.readyState == 4) {
              if(xhr.status == 200) {
                   msg.value="";
              }
         }     
    };
    xhr.send();
};

</script>
</body>
</html>